import Toast from '@vant/weapp/toast/toast';
const app = getApp()

const request = (url, options) => {

  let header = {}
  if (options.method == 'POST') {
    header = {
      'content-type': 'application/x-www-form-urlencoded',
      'Authorization':wx.getStorageSync('Authorization')||''
    }
  } else if(options.method == 'GET') {
    header = {
      'content-type': 'application/json',
      'Authorization':wx.getStorageSync('Authorization')||''
    } 
  }else if(options.method == 'PUT') {
    header = {
      'content-type': 'application/json',
      'Authorization':wx.getStorageSync('Authorization')||''
    }
  }
  return new Promise((resolve, reject) => {
    wx.request({
      // {app.globalData.host}为接口请求中的公共部分写在app.js中
      url: `${app.globalData.host}${url}`,
      method: options.method,
      data: options.method === 'GET' ? options.data : JSON.stringify(options.data) && options.method === 'POST' ? options.data :options.method === 'PUT'? options.data : options.data,
      header: header,
      success(request) {
        if (request.data.code === 0) {
          // Toast.fail('失败文案');
          // console.log(request.data.data,'request.data')
          resolve(request.data.data)
        } else {
          wx.showToast({
            title: request.data.msg,
            icon: "none",
            duration: 1000,
          })
          if(request.data.code==401){
            wx.clearStorageSync()
            wx.showModal({
              title: '登录',
              content: '账号未登录',
              complete: (res) => {
                if (res.confirm) {
                  getCurrentPageUrlWithArgs()
                  wx.switchTab({
                    url: '/pages/my/my',
                  })
                }
              }
            })
          }
          // Toast.fail('失败文案');
          // Notify({ type: 'danger', message: request.data.msg });
          reject(request.data)
        }
      },
      fail(error) {
        // Notify({ type: 'danger', message: request.msg });
        reject(error.data)
      }
    })
  })
}
 /**
   * 获取当前页面路径与参数
   */
  const getCurrentPageUrlWithArgs = () => {
    const pages = getCurrentPages()
    const currentPage = pages[pages.length - 1]
    const url = currentPage.route
    const options = currentPage.options
    let urlWithArgs = `/${url}?`
    for (let key in options) {
      const value = options[key]
      urlWithArgs += `${key}=${value}&`
    }
    urlWithArgs = urlWithArgs.substring(0, urlWithArgs.length - 1)
    wx.setStorageSync('urlWithArgs', urlWithArgs)
    // return urlWithArgs
  }
const get = (url, options = {}) => {
  return request(url, {
    method: 'GET',
    data: options
  })
}
const post = (url, options) => {
  return request(url, {
    method: 'POST',
    data: options
  })
}
const put = (url, options) => {
  return request(url, {
    method: 'PUT',
    data: options
  })
}
// 不能声明DELETE（关键字）
const remove = (url, options) => {
  return request(url, {
    method: 'DELETE',
    data: options
  })
}

module.exports = {
  get,
  post,
  put,
  remove
}